package com.njsf.controller;

import com.njsf.pay.Alipay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@Controller
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private Alipay alipay;


    @RequestMapping("/alipay")
    public void pay(HttpServletResponse response, Map<String, Map<String, String>> orderInfo) {
        try {
            alipay.tradePay(response, orderInfo.get("orderInfo"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 支付完成后同步回调函数
     */
    @RequestMapping("/return_url")
    public String returnUrl(HttpServletRequest request, HttpServletResponse response) {
        try {
            boolean res = alipay.synNotify(request, response);

            if (res) {
                // 支付成功
                return "redirect:/front/orderlist";
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 支付失败
        return "front/payFailed";
    }

    /**
     * 支付完成后异步回调函数
     */
    @RequestMapping("/notify_url")
    public void notifyUrl() {
        System.out.println();
    }

}
